#include "bsp_timer.h"

#ifdef GUICONF_H
#include "GUI.h"

extern __IO GUI_TIMER_TIME OS_TimeMS;
int32_t count_time = 0;

#endif
//#include "lvgl.h"

void ModBus_Timer_Init(uint16_t us_50)
{
	timer_parameter_struct timer_param;

    rcu_periph_clock_enable(MODBUS_TIMER_PERIPH);
    timer_deinit(MODBUS_TIMER);	
	
	timer_param.clockdivision = TIMER_CKDIV_DIV1;
	timer_param.prescaler = 2999;	//50 us
	timer_param.period = us_50;
	timer_param.alignedmode = TIMER_COUNTER_EDGE;
	timer_param.counterdirection = TIMER_COUNTER_UP;
	timer_param.repetitioncounter = 0;
	timer_init(MODBUS_TIMER, &timer_param);
	
	nvic_irq_enable(MODBUS_TIMER_IRQn, 15, 0);
	timer_interrupt_enable(MODBUS_TIMER, TIMER_INT_UP);
}

void ModBus_Timer_Enable(void)
{
	timer_counter_value_config(MODBUS_TIMER , 0);
	timer_enable(MODBUS_TIMER);
}
void ModBus_Timer_Disable(void)
{
	timer_disable(MODBUS_TIMER);
}

void GUI_TIMER_Init(void)
{
	timer_parameter_struct timer_param;

    rcu_periph_clock_enable(GUI_TIMER_PERIPH);
    timer_deinit(GUI_TIMER);	
	
	timer_param.clockdivision = TIMER_CKDIV_DIV1;
	timer_param.prescaler = 59;
	timer_param.period = 999;
	timer_param.alignedmode = TIMER_COUNTER_EDGE;
	timer_param.counterdirection = TIMER_COUNTER_UP;
	timer_param.repetitioncounter = 0;
	timer_init(GUI_TIMER, &timer_param);
	
	nvic_irq_enable(GUI_TIMER_IRQn, 5, 0);
	timer_interrupt_enable(GUI_TIMER, TIMER_INT_UP);
	timer_enable(GUI_TIMER);
}

void GUI_TIMER_IRQHandler(void)
{
	if (timer_interrupt_flag_get(TIMER3, TIMER_INT_UP) == SET)
	{

		timer_interrupt_flag_clear(TIMER3, TIMER_INT_UP);
#if GUICONF_H
		OS_TimeMS++;
		count_time++;
		if (count_time == 10)
		{
			GUI_TOUCH_Exec();
			count_time = 0;
		}
#endif
//		lv_tick_inc(1);
	}
}
